/**
 * @ClassName: UserController
 * $
 * @Date: 2025/4/19 16:06
 */


package com.itheima.mp.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import com.itheima.mp.service.Impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Api(tags="用户管理接口")
@Controller
@RequestMapping("/users")
@RequiredArgsConstructor

public class UserController {

    @Autowired
    private IUserService userService;
    @ApiOperation("新增用户接口")
    @PostMapping
    public  void saveUser(@RequestBody UserFormDTO userDTO) {
        //1.把DTO拷贝到PO中
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        //2.新增用户
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUser(@ApiParam("用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }

    @ApiOperation("扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductMoneyByid(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable("money") Long money) {
        userService.deductionBalance(id,money);
    }

    @ApiOperation("根据复杂条件查询用户接口")
    @GetMapping("/list")
    public List<User> queryUsers(@RequestBody UserQuery userQuery) {
        //1.查询用户PO
        List<User> users= userService.queryUsers(userQuery.getName(),userQuery.getStatus(), userQuery.getMinBalance(),userQuery.getMaxBalance());
        return users;
    }
}
